package com.hec.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hec.yygh.common.result.R;
import com.hec.yygh.hosp.service.DepartmentService;
import com.hec.yygh.hosp.service.HospitalService;
import com.hec.yygh.hosp.service.HospitalSetService;
import com.hec.yygh.hosp.service.ScheduleService;
import com.hec.yygh.hosp.util.HttpRequestHelper;
import com.hec.yygh.hosp.util.MD5;
import com.hec.yygh.model.hosp.Hospital;
import com.hec.yygh.model.hosp.HospitalSet;
import com.hec.yygh.model.hosp.Schedule;
import com.hec.yygh.result.Result;
import com.hec.yygh.vo.hosp.ScheduleQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("/saveHospital")
    public R saveHospital(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
//        log.info("stringObjectMap"+stringObjectMap);
        //校验
        String sign = (String)stringObjectMap.get("sign");
        String platformSignKey = hospitalSetService.getSignKey((String)stringObjectMap.get("hoscode"));
        if(!StringUtils.isEmpty(sign) && !StringUtils.isEmpty(platformSignKey) && MD5.encrypt(platformSignKey).equals(sign)) {

            //传输过程中“+”转换为了“ ”，因此我们要转换回来
            String logoData = (String)stringObjectMap.get("logoData");
            logoData = logoData.replaceAll(" ","+");
            stringObjectMap.put("logoData",logoData);

            hospitalService.save(stringObjectMap);
        }else{
            return R.error();
        }
        return R.ok().code(200);
    }

    //查询医院信息
    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String)stringObjectMap.get("hoscode");
        log.info("hoscode======================="+hoscode);
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //添加科室信息
    @PostMapping("/saveDepartment")
    public Result departmentList(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        log.info("stringObjectMap======================="+stringObjectMap);
        departmentService.saveDepartmentList(stringObjectMap);
        return Result.ok();
    }

    //分页查询科室信息
    @PostMapping("/department/list")
    public Result getDepartmentPage(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        Page page = departmentService.findDepartmentPage(stringObjectMap);
        return Result.ok(page);
    }

    //删除科室信息
    @PostMapping("/department/remove")
    public Result removeDepartmentByDepcode(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        log.info("stringObjectMap==========="+stringObjectMap);
        departmentService.removeDepartmentByDepcode(stringObjectMap);
        return Result.ok();
    }

    //上传排班信息
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        log.info("stringObjectMap================"+stringObjectMap);
        scheduleService.saveSchedule(stringObjectMap);
        return Result.ok();
    }

    //查询排班信息
    @PostMapping("/schedule/list")
    public Result getSchedulePage(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //删除排班信息
    @PostMapping("/schedule/remove")
    public Result removeByHosScheduleId(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        log.info("hosScheduleId============>"+hosScheduleId);
        scheduleService.deleteScheduleByHosScheduleId(hosScheduleId);
        return Result.ok();
    }
}
